<?php

namespace app\advertisement\logic;

use app\advertisement\model\Advaccount as AdvaccountModel;
use think\facade\Db;
use app\data\util\HttpUtil;
use ba\Exception;
use think\facade\Cache;
use app\common\library\Excel;

class AdvaccountLogic
{
    protected $model = null;

    public function __construct()
    {
        $this->model = new AdvaccountModel();
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 列表
     * @author zhangheng
     */
    public function AdvaccountList($params = [])
    {


      //  $data = Cache::store('redis')->get('AdvaccountList');
      //  if (empty($data)) {

        // $data = Cache::store('redis')->get('AdvaccountList');
        // if (empty($data)) {

            $data = Db::name('advaccount')->order('id desc')->select()->toarray();
            // if (!empty($data)) {    
                // Cache::store('redis')->set('AdvaccountList', $data, 3600 * 24);
            // }
        // }
        return json(DefaultResponse(200, true, '获取成功', $data));
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 运营列表
     * @author zhangheng
     */
    public function OperationsList($params = [])
    {
        // $data = Cache::store('redis')->get('OperationsList');
        // if (empty($data)) {
            $data = Db::name('admin')
                ->field('a.id,a.nickname,a.mobile')
                ->alias('a')
                ->join('ba_admin_group_access b','b.uid = a.id', 'left')
                ->where('b.group_id',11)
                ->select()
                ->toarray();
            // if (!empty($data)) {
            //     Cache::store('redis')->set('OperationsList', $data, 3600 * 5);
            // }
        // }
        $return['list'] = $data;
        return json(DefaultResponse(200, true, '获取成功', $return));
    }
    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @ShopList 店铺列表
     * @author zhangheng
     */
    public function ShopList($params = [])
    {

        $data = Db::name('shops')
                ->field('id as shop_id,name as shop_name,seller_type')
                ->where('status','normal')
                ->select()
                ->toarray();
        $return['list'] = $data;
        return json(DefaultResponse(200, true, '获取成功', $return));
    }
    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 列表
     * @author zhangheng
     */
    public function index($params = [])
    {
        $advertiser_id = $params['advertiser_id'] ?? [];
        $month = $params['month'] ?? '';
        $limit = $params['limit'] ?? 10;
        $where = [];

        if (empty($month)) {
            $thismonth = date('m');
            $thisyear = date('Y');
            //默认显示上个月
            // $lastmonth = $thismonth == 1 ? 12 : $thismonth - 1;
            // $lastyear = $thismonth == 1 ? $thisyear - 1 : $thisyear;
            // $month = $lastyear . '-' . str_pad($lastmonth, 2, '0', STR_PAD_LEFT);
            //修改成默认显示成当月
            $month = $thisyear . '-' . $thismonth;
        }

        if (!empty($advertiser_id)) {
            $where[] = ['a.id', 'in', $advertiser_id];
        }

        $stat_time_day_last = date('Y-m-d 00:00:00', strtotime($month));
        $stat_time_day_new = date('Y-m-d 00:00:00', strtotime('+1 month', strtotime($month)));
        $list = Db::name('advaccount')
            ->field('a.*, sum(b.spend) as spend')
            ->alias('a')
            ->join('ba_advertising b', 'b.advertiser_id = a.advertiser_id', 'left')
            ->join('ba_advaccount_admin c', 'c.advertiser_id = a.id', 'left')
            ->where($where)
            ->where('b.stat_time_day', '>=', $stat_time_day_last)
            ->where('b.stat_time_day', '<', $stat_time_day_new)
            // ->where('c.status', '=', 'normal')
            ->group('a.advertiser_id')
            ->order('spend desc')
            ->paginate($limit);
        $lists = $list->items();
        foreach ($lists as &$item) {
            $item['spend'] = round($item['spend'], 3);
            $item['month'] = $month;
        }

        $data = [
            'list' => $lists,
            'total' => $list->total(),
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 导出
     * @author zhangheng
     */
    public function export($params = [])
    {
        $advertiser_id = $params['advertiser_id'] ?? [];
        $month = $params['month'] ?? '';
        $where = [];

        if (empty($month)) {
            $thismonth = date('m');
            $thisyear = date('Y');
            //默认显示上个月
            // $lastmonth = $thismonth == 1 ? 12 : $thismonth - 1;
            // $lastyear = $thismonth == 1 ? $thisyear - 1 : $thisyear;
            // $month = $lastyear . '-' . str_pad($lastmonth, 2, '0', STR_PAD_LEFT);
            //修改成默认显示成当月
            $month = $thisyear . '-' . $thismonth;
        }

        if (!empty($advertiser_id)) {
            $where[] = ['a.id', 'in', $advertiser_id];
        }

        $stat_time_day_last = date('Y-m-d 00:00:00', strtotime($month));
        $stat_time_day_new = date('Y-m-d 00:00:00', strtotime('+1 month', strtotime($month)));
        $lists = Db::name('advaccount')
            ->field('a.*, sum(b.spend) as spend')
            ->alias('a')
            ->join('ba_advertising b', 'b.advertiser_id = a.advertiser_id', 'left')
            ->join('ba_advaccount_admin c', 'c.advertiser_id = a.id', 'left')
            ->where($where)
            ->where('b.stat_time_day', '>=', $stat_time_day_last)
            ->where('b.stat_time_day', '<', $stat_time_day_new)
            // ->where('c.status', '=', 'normal')
            ->group('a.advertiser_id')
            ->order('spend desc')
            ->select()
            ->toarray();
        foreach ($lists as &$item) {
            $item['spend'] = round($item['spend'], 3);
            $item['month'] = $month;
        }

        $header[] = ['广告账户', 'advertiser_id'];
        $header[] = ['广告名称', 'advertiser_name'];
        $header[] = ['花费', 'spend'];
        $header[] = ['统计月份', 'month'];
        $fileName = "export_advaccount" . time();
        return Excel::exportData($lists, $header, $fileName, 'xlsx');
    }
    
}
